﻿define([
    'dojo/_base/declare',
    'geomap/plugins/layers/ExGraphicsLayer',
    'esri/symbols/PictureFillSymbol',
    'esri/symbols/SimpleFillSymbol',
    'esri/geometry/Polygon',
    'esri/graphic',
], function (declare, GraphicsLayer, PictureFillSymbol, SimpleFillSymbol, Polygon, Graphic) {
    var PolygonLayer = declare([GraphicsLayer], {
        setData: function (mapUtils, layer) {
            var me = this;
            me.clear();
            if (!mapUtils) {
                return;
            }
            me.mapUtils = mapUtils;
            me._setLayer(layer);
            var options = layer.layer || {};
            me.options = options;
            var data = Object.assign([], layer.layer.data);

            me.highlightOptions = Object.assign(
                {
                    visible: false,
                    symbol: {
                        type: 'simplePolygon', //面样式类型
                        style: 'solid', //面填充类型
                        color: [0, 255, 255, 50], //面填充色
                        outline: {
                            color: [0, 255, 255], //边界颜色
                            style: 'solid', //边界线型,如果不设置，默认是solid
                            width: 2, //边界宽度
                        },
                    },
                },
                options.highlightOptions
            );

            me.popupOptions = Object.assign(
                {
                    visible: false,
                    isMultiPopup: false,
                    contentField: 'content',
                    isShowCloseIcon: false,
                },
                options.popupOptions
            );
            me.__currentGraphics = [];
            if (me.__mapOnLoadHandle) {
                clearInterval(me.__mapOnLoadHandle);
            }
            me.__mapOnLoadHandle = setInterval(function () {
                if (mapUtils.getMap().loaded) {
                    clearInterval(me.__mapOnLoadHandle);
                    if (!data || data.length < 1) {
                        if (typeof options.callback == 'function') {
                            options.callback(layer);
                        }
                    } else {
                        var symbolFieldName = options.symbolFieldName || 'symbol';
                        var ringsFieldName = options.ringsFieldName || 'rings';
                        var spatialReference = me.spatialReference;

                        data.map(function (item, index) {
                            if (ringsFieldName) {
                                item.rings = item[ringsFieldName];
                            }
                            var polygon = me.createPolygon(item, {
                                sr: spatialReference,
                            });
                            var symbolItem = item[symbolFieldName] || options.symbol;
                            let symbol = me.createSymbol(symbolItem);
                            var attributes = item;
                            if (!item.x || !item.y) {
                                var center = polygon && polygon.getCentroid();
                                if (center) {
                                    item.x = center.x;
                                    item.y = center.y;
                                }
                            }
                            var graphic = me.createGraphic({
                                geometry: polygon,
                                symbol: symbol,
                                attributes: attributes,
                            });
                            me.add(graphic);

                            if (index == data.length - 1) {
                                me._calCurrentGraphics();

                                if (options.labelOptions) {
                                    me.labelOptions = Object.assign(
                                        {
                                            textField: 'name',
                                            ptThreshold: 0, //当前窗口的点位少于这个最大值时，标签强制展示
                                            showLabels: 1, //1:根据比例尺展示，2：强制展示，0：强制不展示
                                            scaleInfo: { maxScale: 0, minScale: 0 },
                                            labelSymbol: {
                                                labelClass: 'labelInfo',
                                                labelStyle: {
                                                    offsety: 18,
                                                    offsetx: 0,
                                                    position: 'top',
                                                },
                                            },
                                        },
                                        options.labelOptions
                                    );
                                    me._showLabels(mapUtils, options);
                                }
                                if (options.popupOptions && options.popupOptions.visible) {
                                    me.openCustomPopup(mapUtils, data);
                                }
                                if (typeof options.callback == 'function') {
                                    options.callback(layer);
                                }
                            }
                        });

                        // if (options.highlightOptions) {
                        me._showHighlight(mapUtils, options);
                        //}
                        //;
                    }
                }
            });
        },
        getGraphicLocation: function (esriEvent) {
            if (!esriEvent || !esriEvent.mapPoint) {
                return;
            }
            var geometry = esriEvent.mapPoint || {};
            var lngFieldName = 'x';
            var latFieldName = 'y';
            var location = {};
            location[lngFieldName] = geometry.x;
            location[latFieldName] = geometry.y;
            location.sr = geometry.spatialReference;
            return location;
        },
        /*鼠标移入后设置高亮样式*/
        _setHighlightSymbol: function (e) {
            if (!this.highlightOptions || !this.highlightOptions.visible) {
                return;
            }
            if (!e.graphic._defaultSymbol) {
                e.graphic._defaultSymbol = e.graphic.symbol;
            }
            var symbol = e.graphic.symbol;
            if (this.highlightOptions.symbol) {
                symbol = this.createSymbol(this.highlightOptions.symbol);
                e.graphic.setSymbol(symbol);
            }
        },
        /*鼠标移出后还原样式*/
        _setDefaultSymbol: function (e) {
            if (!this.highlightOptions || !this.highlightOptions.visible) {
                return;
            }
            var symbol = e.graphic._defaultSymbol;
            if (!symbol) {
                return;
            }
            e.graphic.setSymbol(symbol);
        },
        _creatLabelData: function (mapUtils, graphics) {
            var me = this;
            if (!mapUtils) {
                return;
            }
            var map = mapUtils.getMap();
            if (!map) {
                return;
            }
            if (!me.labelOptions) {
                return;
            }
            var symbol = me.labelOptions.labelSymbol;

            var labelClass = me._changeSymbol(symbol.labelClass, mapUtils);

            me.__textGraphics = [];
            graphics.map(function (item) {
                if (item._shape) {
                    var attributes = JSON.parse(JSON.stringify(item.attributes));
                    if (attributes) {
                        if (!attributes.x || !attributes.y) {
                            var xy = item.geometry.getCentroid();
                            attributes.x = xy.x;
                            attributes.y = xy.y;
                        }
                        var label = attributes[me.labelOptions.textField];
                        if (label && label !== '--') {
                            attributes.symbol = attributes.labelSymbol || {
                                html: `<div class="${labelClass}">${label}</div>`,
                            };
                            me.__textGraphics.push(attributes);
                        }
                    }
                }
            });
        },
        createSymbol(t = { type: Null }) {
            if (t.type == 'esriSFS') {
                return new SimpleFillSymbol(t);
            } else if (t.type == 'esriPFS') {
                return new PictureFillSymbol(t);
            } else {
                let symbol = new SimpleFillSymbol({
                    type: 'esriSFS',
                    style: 'esriSFSSolid',
                    color: [115, 76, 0, 255],
                    outline: {
                        type: 'esriSLS',
                        style: 'esriSLSSolid',
                        color: [110, 110, 110, 255],
                        width: 2,
                    },
                });
                return symbol;
            }
        },
        createGraphic(t) {
            t = Object.assign(
                { geometry: null, symbol: null, attributes: null, infoTemplate: null },
                t
            );
            let g = new Graphic(t.geometry, t.symbol, t.attributes, t.infoTemplate);
            return g;
        },
        createPolygon(t, a = {}) {
            var o = a.ringsFieldName || 'rings',
                i = a.sr || this.mapUtils.getSpatialReference();
            let g = new Polygon({
                rings: t[o],
                spatialReference: i,
            });
            return g;
        },
    });
    return PolygonLayer;
});
